package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catalog2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 页面跳转的控制层
 */
@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;

    /**
     * 首页的页面跳转
     *
     * @return
     */
    @GetMapping(value = {"/", "/index", "index.html"})
    public String index(Model model) {
        List<CategoryEntity> categoryEntityList = categoryService.getLevel1Categorys();
        model.addAttribute("categorys", categoryEntityList);
        // 返回逻辑页面访问地址
        // 视图解析器进行拼串 classpath:/templates/ + index + .html
        return "index";
    }


    /**
     * 获取分类数据，返回json
     *
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/index/json/catalog.json")
    public Map<String, List<Catalog2Vo>> getCatalogJson() {
        Map<String, List<Catalog2Vo>> catalogJson = categoryService.getCatalogJsonFromRedis();
        return catalogJson;
    }


}
